//
// Created by 123 on 2025/1/10.
//
#include "string"
#include "iostream"

class MyAdd{
public:
    int operator()(int v1,int v2){
        return v1 + v2;
    }
};
//1、函数对象在使用时，可以像普通函数那样调用, 可以有参数，可以有返回值
void test(){
    MyAdd myAdd;
    std::cout << myAdd(10,10) << std::endl;
}

class MyPrint{
public:
    int count;
public:
    MyPrint(){
        this->count = 0;
    }
    void operator()(std::string test){
        std::cout << test << std::endl;
        //统计使用次数
        count++;
    }
};
//2、函数对象可以有自己的状态
void test2(){
    MyPrint myPrint;
    myPrint("hello1");
    myPrint("hello2");
    myPrint("hello3");
    myPrint("hello4");
    std::cout << "myPrint invoke count=" << myPrint.count << std::endl;
}
//3函数对象作为参数传递
void doPrint(MyPrint& mp,std::string test){
    mp(test);
}

void test3(){
    MyPrint myPrint;
    doPrint(myPrint,"fuc");
}

int main145(){
    test();
    test2();
    test3();
    return 0;
}